package br.com.sipe.DAO.impl;

import java.util.List;
import java.util.Map;
import java.util.Set;

import org.hibernate.Criteria;
import org.hibernate.SessionFactory;
import org.hibernate.criterion.MatchMode;
import org.hibernate.criterion.Projections;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import org.springframework.stereotype.Repository;

import br.com.arquitetura.DAO.UniversalDAO;
import br.com.arquitetura.util.CriteriaMC;
import br.com.sipe.DAO.ParticipanteDAO;
import br.com.sipe.entidade.Participante;

@Repository(value = "ParticipanteDAO")
public class ParticipanteDAOImpl extends HibernateDaoSupport implements ParticipanteDAO {

	@Autowired(required = true)
	public void setFactory(SessionFactory factory) {
		super.setSessionFactory(factory);
	}

	@Autowired(required = true)
	@Qualifier(value = "universalDAO")
	protected UniversalDAO dao;

	@Override
	public Long countParticipante(Participante model, Map filters) {
		Criteria c = retornarCriteria(model, filters);
		c.setProjection(Projections.rowCount());

		Long result = (Long) c.list().get(0);
		return result;
	}

	@Override
	public List<Participante> paginateParticipante(int first, int pageSize, String sortField, boolean sort, Participante model, Map filters) {
		Criteria c = retornarCriteria(model, filters);

		if (first != 0)
			c.setFirstResult(first);
		if (pageSize != 0)
			c.setMaxResults(pageSize);

		CriteriaMC criteriaMC = new CriteriaMC();
		criteriaMC.setCriteria(c);

		// Adicionando Sorting
		criteriaMC = dao.addOrderBy(criteriaMC, sortField, sort);

		return criteriaMC.getCriteria().list();
	}

	public Criteria retornarCriteria(Participante model, Map<String, String> filters) {
		if (model == null) {
			return null;
		}

		Criteria criteria = getSession().createCriteria(model.getClass());
		if (filters != null) {
			Set<String> chaves = filters.keySet();
			for (String fil : chaves) {
				if (fil.trim().equalsIgnoreCase("areaAtuacao.descricao")) {
					criteria.createAlias("areaAtuacao", "a");
					criteria.add(Restrictions.ilike("a.descricao", filters.get(fil), MatchMode.ANYWHERE));
				}
				if (fil.trim().equalsIgnoreCase("nome")) {
					criteria.add(Restrictions.ilike("nome", filters.get(fil), MatchMode.ANYWHERE));
				}
				if (fil.trim().equalsIgnoreCase("email")) {
					criteria.add(Restrictions.ilike("email", filters.get(fil), MatchMode.ANYWHERE));
				}
				if (fil.trim().equalsIgnoreCase("cpf")) {
					criteria.add(Restrictions.ilike("cpf", filters.get(fil), MatchMode.START));
				}
				if (fil.trim().equalsIgnoreCase("municipio.uf.sigla")) {
					criteria.createAlias("municipio", "m");
					criteria.createAlias("m.uf", "u");
					criteria.add(Restrictions.ilike("u.sigla", filters.get(fil), MatchMode.START));
				}
				if (fil.trim().equalsIgnoreCase("municipio.descricao")) {
					criteria.createAlias("municipio", "m");
					criteria.add(Restrictions.ilike("m.descricao", filters.get(fil), MatchMode.ANYWHERE));
				}
			}
		}

		return criteria;
	}

}
